/***************************************************************************
 *   Copyright (C) 2004 by Nilesh Bansal                                   *
 *   nilesh@iitb.ac.in                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef FTP_MONITOR_LOOP_H
#define FTP_MONITOR_LOOP_H

#include <QObject>
#include <QProcess>
#include <QTimer>
#include <QVector>

/**
 * FTPMonitorLoop encapsulate start of "who" command, create "who" command and it's arguaments and don't know nothing
 * about KConfigGroup, all settings save/load are doing through the FTPMonitor class
 */
class FTPMonitorLoop : public QObject
{
Q_OBJECT
public:
	enum FtpServerType {
		proftpd=1,pureftpd=2,ncftpd=3,vsftpd=4
	};

	struct FtpWhoCommand{
		QString bin;
		QStringList args;
	};

	FTPMonitorLoop(FtpServerType ftpServer,QString whoPath,bool useSudo);
	~FTPMonitorLoop();
	
	void start(int interval,bool allowPopup);
	void stop();
	void createExecCommand(FtpServerType ftpServer,bool useSudo,QString whoPath);
	
	void setFtpServer(FtpServerType ftpServer){	mFtpDaemon=ftpServer;}
	FtpServerType ftpServer(){	return mFtpDaemon;}
private slots:
	void execWho();
        void readFromStdout();
        void readFromStdoutNcFtpd();
        void readFromStdoutVsFtpd();
        void readFromStdoutPureFtpd();
        void readFromStdoutProFtpd();

private:
	void checkClientRows(QVector<QStringList> listOfClients);
	bool checkChangeForClient(QStringList newClientRow,QStringList oldClientRow);
	QVector<QStringList> mListOfClients;
	
	FtpWhoCommand mFtpWhoCommand;
	
	FtpServerType mFtpDaemon;
	bool mAllowPopup;
	
	QProcess *mProc;
	QProcess *mProcGrep;
	
	QTimer *mTimer;
signals:
	void sigNewClientsConnected(int count);
	void sigAddRow(QStringList row);
	void sigDelRow(int row);
	void sigChangeRow(QStringList,int);
	void sigClientsCountChanged(int);
};
#endif
